package com.caifu.controller;

import com.caifu.bean.Result;
import com.caifu.bean.ResultUtil;
import com.caifu.service.ActiveMqService;
import com.caifu.service.CDRecordService;
import com.caifu.service.CUserRecordService;
import com.caifu.service.UserDeviceService;
import com.caifu.util.Constance;
import com.caifu.util.HttpUtils;
import com.caifu.util.tools.DateUtils;
import com.caifu.vo.IssuedVo;
import com.caifu.vo.UserRecordVo;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("device/user")
public class DeviceUserController extends AbstractController {
    final static Logger logger = LoggerFactory.getLogger(DeviceUserController.class);

    @Resource
    private ActiveMqService activeMqService;

    @Resource
    private UserDeviceService userDeviceService;

    @Resource
    private CDRecordService cdRecordService;

    @Resource
    private CUserRecordService cUserRecordService;


    /**
     * 下发人员信息
     */
    @GetMapping("addUser")
    @ResponseBody
    public Result addUser(String deviceId) {
        try {
            Map map = activeMqService.addMqMessage(deviceId);
            return ResultUtil.success(200, "", map);
        } catch (Exception e) {
            String msg = "网络异常";
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    /**
     * 下发人员信息反馈接口
     */
    @RequestMapping("reciveUser")
    @ResponseBody
    public Result reciveUser(HttpServletRequest request) {
        try {
            String requestData = Constance.readAsChars(request);
            if(StringUtils.isNotBlank(requestData) && HttpUtils.isJsonObject(requestData)) {
                JSONObject obj = JSONObject.fromObject(requestData);
//                System.out.println(obj);
                JSONObject jsonObject = JSONObject.fromObject(obj.get("params"));
                JSONObject json = JSONObject.fromObject(jsonObject.get("nameValuePairs"));
                List<IssuedVo> list = JSONArray.toList(JSONArray.fromObject(json.get("list")),IssuedVo.class);
                Map map = cdRecordService.editRecordList(list);
                if("200".equals(map.get("code"))){
                    return ResultUtil.success(200, "接收成功！");
                } else {
                    return ResultUtil.error(-1, "反馈数据异常！");
                }
            } else {
                return ResultUtil.error(-1, "参数解析错误");
            }
        } catch (Exception e) {
            String msg = "网络异常";
            logger.error(e.toString());
            e.printStackTrace();
            return ResultUtil.error(500, msg);
        }
    }

    /**
     * 人员开门记录接口
     */
    @RequestMapping("reciveRecord")
    @ResponseBody
    public Result reciveRecord(HttpServletRequest request) {
        try {
            String requestData = Constance.readAsChars(request);
            if(StringUtils.isNotBlank(requestData) && HttpUtils.isJsonObject(requestData)) {
                JSONObject obj = JSONObject.fromObject(requestData);
                System.out.println(obj);
                JSONObject jsonObject = JSONObject.fromObject(obj.get("params"));
                JSONObject json = JSONObject.fromObject(jsonObject.get("nameValuePairs"));
                List<UserRecordVo> list = JSONArray.toList(JSONArray.fromObject(json.get("list")), UserRecordVo.class);
                Map map = cUserRecordService.addUserRecord(list);
                if("200".equals(map.get("code"))){
                    return ResultUtil.success(200, "接收成功！");
                } else {
                    return ResultUtil.error(-1, "反馈数据异常！");
                }
            } else {
                return ResultUtil.error(-1, "参数解析错误");
            }
        } catch (Exception e) {
            String msg = "网络异常";
            logger.error(e.toString());
            e.printStackTrace();
            return ResultUtil.error(500, msg);
        }
    }


    /**
     * 访客密码校验
     */
    @RequestMapping("checkGuestCode")
    @ResponseBody
    public Result checkGuestCode(String sn, String code) {
        try {
            if(sn == null || "".equals(sn)){
                return ResultUtil.error(301, "设备编号不能为空!");
            }
            if(code == null || "".equals(code)){
                return ResultUtil.error(301, "验证码不能为空!");
            }
            System.out.println(code);
            Map map = userDeviceService.checkGuestCode(sn, code, DateUtils.currentDateTime());

            int reCode = Integer.valueOf(map.get("code").toString());

            if(reCode == 0){
                return ResultUtil.success(reCode, "校验通过！");
            } else {
                return ResultUtil.error(reCode, map.get("msg").toString());
            }

        } catch (Exception e) {
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, "网络异常");
        }
    }





}
